package com.tjec.project.system.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.tjec.common.utils.StringUtils;
import com.tjec.framework.web.domain.TreeSelect;
import com.tjec.project.projectLeaderWorkbeanch.domain.BmsMdExpertType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tjec.project.system.mapper.BmsMdAreaMapper;
import com.tjec.project.system.domain.BmsMdArea;
import com.tjec.project.system.service.IBmsMdAreaService;

/**
 * 省市区Service业务层处理
 * 
 * @author cjj
 * @date 2020-04-28
 */
@Service
public class BmsMdAreaServiceImpl implements IBmsMdAreaService 
{
    @Autowired
    private BmsMdAreaMapper bmsMdAreaMapper;

    /**
     * 查询省市区
     * 
     * @param guid 省市区guid
     * @return 省市区
     */
    @Override
    public BmsMdArea selectBmsMdAreaByGuid(String guid)
    {
        return bmsMdAreaMapper.selectBmsMdAreaByGuid(guid);
    }

    /**
     * 查询省市区
     *
     * @param guids 省市区guid集合
     * @return 省市区
     */
    @Override
    public List<BmsMdArea> selectBmsMdAreaByGuids(String[] guids){
        return bmsMdAreaMapper.selectBmsMdAreaByGuids(guids);
    }

    /**
     * 查询省市区列表
     * 
     * @param bmsMdArea 省市区
     * @return 省市区
     */
    @Override
    public List<BmsMdArea> selectBmsMdAreaList(BmsMdArea bmsMdArea)
    {
        return bmsMdAreaMapper.selectBmsMdAreaList(bmsMdArea);
    }

    /**
     * 新增省市区
     * 
     * @param bmsMdArea 省市区
     * @return 结果
     */
    @Override
    public int insertBmsMdArea(BmsMdArea bmsMdArea)
    {
        return bmsMdAreaMapper.insertBmsMdArea(bmsMdArea);
    }

    /**
     * 修改省市区
     * 
     * @param bmsMdArea 省市区
     * @return 结果
     */
    @Override
    public int updateBmsMdArea(BmsMdArea bmsMdArea)
    {
        return bmsMdAreaMapper.updateBmsMdArea(bmsMdArea);
    }

    /**
     * 批量删除省市区
     * 
     * @param guids 需要删除的省市区guid
     * @return 结果
     */
    @Override
    public int deleteBmsMdAreaByGuids(String[] guids)
    {
        return bmsMdAreaMapper.deleteBmsMdAreaByGuids(guids);
    }

    /**
     * 删除省市区信息
     * 
     * @param guid 省市区guid
     * @return 结果
     */
    @Override
    public int deleteBmsMdAreaByGuid(String guid)
    {
        return bmsMdAreaMapper.deleteBmsMdAreaByGuid(guid);
    }

    @Override
    public List<TreeSelect> buildBmsMdAreaTreeSelect(List<BmsMdArea> bmsMdAreas) {
        List<BmsMdArea> expertTypeTrees = buildBmsMdAreaTree(bmsMdAreas);
        return expertTypeTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    @Override
    public List<BmsMdArea> buildBmsMdAreaTree(List<BmsMdArea> bmsMdAreas) {
        List<BmsMdArea> returnList = new ArrayList<>();
        List<String> tempList = new ArrayList<String>();
        for (BmsMdArea bmsMdArea : bmsMdAreas) {
            tempList.add(bmsMdArea.getGuid());
        }
        for (Iterator<BmsMdArea> iterator = bmsMdAreas.iterator(); iterator.hasNext();) {
            BmsMdArea bmsMdArea = (BmsMdArea) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(bmsMdArea.getParentId())) {
                recursionFn(bmsMdAreas, bmsMdArea);
                returnList.add(bmsMdArea);
            }
        }
        if (returnList.isEmpty()) {
            returnList = bmsMdAreas;
        }
        return returnList;
    }
    /**
     * 查询省市区列表
     *
     * @param bmsMdArea 省市
     * @return 省市
     */
    @Override
    public List<BmsMdArea> selectBmsMdAreaListTwo(BmsMdArea bmsMdArea) {
        return bmsMdAreaMapper.selectBmsMdAreaListTwo(bmsMdArea);
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<BmsMdArea> list, BmsMdArea t) {
        // 得到子节点列表
        List<BmsMdArea> childList = getChildList(list, t);
        t.setChildren(childList);
        for (BmsMdArea tChild : childList) {
            if (hasChild(list, tChild)) {
                // 判断是否有子节点
                Iterator<BmsMdArea> it = childList.iterator();
                while (it.hasNext()) {
                    BmsMdArea n = (BmsMdArea) it.next();
                    recursionFn(list, n);
                }
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<BmsMdArea> getChildList(List<BmsMdArea> list, BmsMdArea t) {
        List<BmsMdArea> tlist = new ArrayList<>();
        Iterator<BmsMdArea> it = list.iterator();
        while (it.hasNext()) {
            BmsMdArea n = (BmsMdArea) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && t.getGuid().equals(n.getParentId())) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<BmsMdArea> list, BmsMdArea t) {
        return getChildList(list, t).size() > 0 ? true : false;
    }
}
